package com.scalar.chiptrack.operations.wipflush.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.wipflush.forms.WipFlushSetupForm;
import com.scalar.chiptrack.operations.wipflush.dao.WipFlushDetailDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * User: Sujan
 * Date: Nov 3, 2005
 * Time: 2:27:33 PM
 */
public class WipFlushSetupLoadBinDetailsCommand extends ActionCommand
{
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        WipFlushSetupForm wipFlushSetupForm = (WipFlushSetupForm)form;
        HttpSession session = request.getSession();
        User pUser = ( User )session.getAttribute( SessionKeys.USER_KEY );
        String userId = pUser.getUserId();

        ActionForward actionForward = mapping.findForward( "productFamilyDetails" );

        HashMap errors = (HashMap)request.getAttribute ( ERROR_MESSAGES );
        HashMap info   = ( HashMap )request.getAttribute( INFO_MESSAGES );

        int baseProductId = wipFlushSetupForm.getBaseProductId();
        try
        {
            LinkedHashMap productFamilyDetailsMap = WipFlushDetailDAO.loadProductFamilyDetails( null , baseProductId );
            if ( productFamilyDetailsMap != null && productFamilyDetailsMap.size() > 0 )
            {
                request.setAttribute( "productFamilyDetailsMap", productFamilyDetailsMap );
            }
            else
            {
                System.out.println ( "====CouldNot find base product details map" );
                errors.put( "Could not find Base Product", null);
            }
            wipFlushSetupForm.setBinNumbers( WipFlushDetailDAO.getProcessStepBinNumbers(null, wipFlushSetupForm.getSelectedProcessStepId() ) );
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            errors.put( SessionKeys.ERROR_REPORT_SUBJECT, "Error: Could not find Base Product details");
            errors.put( SessionKeys.ERROR_REPORT, e );
        }
        return actionForward;
    }
}
